<?php
namespace Home\Controller;

use Think\Page;

class CommentsController extends BaseController {
	const LOAD_LIMIT = 8;
    /**
     * 美容师、美容院评论加载
     */
    public function ajaxLoadComments() {
    	$id = I ('get.id');
    
    	$entity = M('AutoId')->where(array('id' => $id))->find();
    
    	$OrderCommentModel = D('OrderComment');
    	$BeauticianModel = D('Beautician');
    	 
    	$type = I ('get.type');
    	if ($type == 'good') {
    		$OrderCommentModel->avg_score = array('egt', 4);
    	}
    	 
    	if ($entity ['type'] == 'beautician') {
    		$goodComment = $OrderCommentModel->totalBeauticianComment($id);
    		$page = new Page($goodComment, self::LOAD_LIMIT);
    		$comments = $OrderCommentModel->getBeauticianComments($id, $page->firstRow, $page->listRows);
    		$beautician = $BeauticianModel->getBeauticianInfo($id);
    	} else if ($entity ['type'] == 'beauty_parlor'){
    		$goodComment = $OrderCommentModel->totalBeautyComments($id);
    		$page = new Page($goodComment, self::LOAD_LIMIT);
    		$comments = $OrderCommentModel->getBeautyComments($id, $page->firstRow, $page->listRows);
    	}
    	 
    	if ($comments) {
    		$UserModel = D('User');
    		foreach ($comments as &$comment) {
    			$userInfo = $UserModel->getUserById($comment ['uid']);
    			if ($userInfo) {
    				$userInfo ['mobile'] = hidden_phone_word($userInfo ['mobile']);
    				$comment ['user_info'] = $userInfo;
    			}
    			 
    			$comment ['commentTime'] = date('Y-m-d H:i:s', $comment ['createtime']);
    			$comment ['start'] = floor($comment ['avg_score']);
    			$commentPhoto = $OrderCommentModel->getCommentPhoto($comment ['id']);
    			if ($commentPhoto) {
    				$comment ['photos'] = $commentPhoto;
    			}
    			if ($entity ['type'] == 'beauty_parlor'){
    				$comment ['beautician'] = $BeauticianModel->getBeauticianInfo($reply ['uid']);
    			} else {
    				$comment ['beautician'] = $beautician;
    			}
    			
    			$reply = $OrderCommentModel->where(array('parentid' => $comment ['id']))->find();
    			if ($reply) {
    				$replyPhoto = $OrderCommentModel->getCommentPhoto($reply ['id']);
    				if ($replyPhoto) {
    					$reply ['photos'] = $replyPhoto;
    				}
    
    				if ($entity ['type'] == 'beauty_parlor'){
    					$reply ['beautician'] = $BeauticianModel->getBeauticianInfo($reply ['uid']);
    				} else {
    					$reply ['beautician'] = $beautician;
    				}
    
    				$reply ['replyTime'] = date('Y-m-d H:i:s', $reply ['createtime']);
    				$comment ['reply'] = $reply;
    			}
    		}
    	}
    	 
    	$pageNumber = I('get.p')?I('get.p'):1;
    	$hasMore = false;
    	$nextPage = false;
    	if ($goodComment > $pageNumber * self::LOAD_LIMIT) {
    		$hasMore = true;
    		$nextPage = U('/Comments/ajaxLoadComments/id/'.$id.($type == 'good'?'/type/good':'').'/p/'. ($pageNumber + 1));
    	}
    	
    	$this->ajaxReturn(array(
    		'status' => 1, 
    		'data' => $comments, 
    		'hasMore' => $hasMore,
    		'nextPage' => $nextPage,
    	));
    }
    
    /**
     * 评论数据的详细信息
     */
    public function commentDetail() {
    	$id = I('get.id');
    	 
    	$OrderCommentModel = D('OrderComment');
    	$comment = $OrderCommentModel->getCommentInfo($id);
    	 
    	$comment ['start'] = floor($comment ['avg_score']);
    	$commentPhoto = $OrderCommentModel->getCommentPhoto($comment ['id']);
    	if ($commentPhoto) {
    		$comment ['photos'] = $commentPhoto;
    	}
    	 
    	$BeauticianModel = D('Beautician');
    	if ($comment ['parentid'] == '0') {
    		$UserModel = D('User');
    		$userInfo = $UserModel->getUserById($comment ['uid']);
    		if ($userInfo) {
    			$comment ['user_info'] = $userInfo;
    		}
    
    		$reply = $OrderCommentModel->where(array('parentid' => $comment ['id']))->find();
    		if ($reply) {
    			$replyPhoto = $OrderCommentModel->getCommentPhoto($reply ['id']);
    			if ($replyPhoto) {
    				$reply ['photos'] = $replyPhoto;
    			}
    
    			$reply ['beautician'] = $BeauticianModel->getBeauticianInfo($reply ['uid']);
    			$comment ['reply'] = $reply;
    		}
    
    		$this->assign('comment', $comment);
    		$this->display('user_comment_detail');
    	} else {
    		$beautician = $BeauticianModel->getBeauticianInfo($comment ['uid']);
    
    		$this->assign('comment', $comment);
    		$this->assign('beautician', $beautician);
    
    		$this->display('beauty_comment_reply');
    	}
    }
    
    /**
     * 美容师、美容院的评论页面的初始化
     */
    public function index() {
    	$id = I ('get.id');
    	 
    	$entity = M('AutoId')->where(array('id' => $id))->find();
    	 
    	$OrderCommentModel = D('OrderComment');
    	$BeauticianModel = D('Beautician');
    	if ($entity ['type'] == 'beautician') {
    		$allComment = $OrderCommentModel->totalBeauticianComment($id);
    
    		$page = new Page($allComment, self::LOAD_LIMIT);
    		$comments = $OrderCommentModel->getBeauticianComments($id, $page->firstRow, $page->listRows);
    
    		$OrderCommentModel->avg_score = array('egt', 4);
    		$goodComment = $OrderCommentModel->totalBeauticianComment($id);
    
    		$beautician = $BeauticianModel->getBeauticianInfo($id);
    	} else if ($entity ['type'] == 'beauty_parlor'){
    		$allComment = $OrderCommentModel->totalBeautyComments($id);
    
    		$page = new Page($allComment, self::LOAD_LIMIT);
    		$comments = $OrderCommentModel->getBeautyComments($id, $page->firstRow, $page->listRows);
    
    		$OrderCommentModel->avg_score = array('egt', 4);
    		$goodComment = $OrderCommentModel->totalBeautyComments($id);
    	}
    	 
    	 
    	if ($comments) {
    		$UserModel = D('User');
    		foreach ($comments as &$comment) {
    			$userInfo = $UserModel->getUserById($comment ['uid']);
    			if ($userInfo) {
    				$comment ['user_info'] = $userInfo;
    			}
    			 
    			$comment ['start'] = floor($comment ['avg_score']);
    			$commentPhoto = $OrderCommentModel->getCommentPhoto($comment ['id']);
    			if ($commentPhoto) {
    				$comment ['photos'] = $commentPhoto;
    			}
    			 
    			$reply = $OrderCommentModel->where(array('parentid' => $comment ['id']))->find();
    			if ($reply) {
    				$replyPhoto = $OrderCommentModel->getCommentPhoto($reply ['id']);
    				if ($replyPhoto) {
    					$reply ['photos'] = $replyPhoto;
    				}
    
    				if ($entity ['type'] == 'beauty_parlor'){
    					$reply ['beautician'] = $BeauticianModel->getBeauticianInfo($reply ['uid']);
    				} else {
    					$reply ['beautician'] = $beautician;
    				}
    
    				$comment ['reply'] = $reply;
    			}
    		}
    	}
    	 
    	$pageNumber = I('get.p')?I('get.p'):1;
    	$hasMore = false;
    	if ($allComment > $pageNumber * self::LOAD_LIMIT) {
    		$hasMore = true;
    		
    		$nextPage = U('/Comments/ajaxLoadComments/id/'.$id.'/p/'. ($pageNumber + 1));
    		$this->assign('nextPage', $nextPage);
    	}
    	$this->assign('hasMore', $hasMore);
    	$this->assign('id', $id);
    	$this->assign('allComment', $allComment);
    	$this->assign('goodComment', $goodComment);
    	 
    	$this->assign('comments', $comments);
    	$this->display();
    }
    
    /**
     * 订单评论
     */
    public function comment() {
    	$userId = getSessionUserId();
    	if (!IS_POST) {
    		$OrderModel = D('Order');
    		$orderId = I ('get.id');
    		$orderInfo = $OrderModel->getOrderInfo($orderId);
    
    		if ($orderInfo ['status'] != 4) {
    			$this->nodata('用户评价', '服务未使用，不能进行评论');
    		} else {
    			$OrderCommentModel = D('OrderComment');
    			$comment = $OrderCommentModel->getUserOrderComment($userId, $orderId);
    			 
    			$BeauticianModel = D('Beautician');
    			$beauticianInfo = $BeauticianModel->getBeauticianInfo($orderInfo ['beautician_id']);
    			 
    			$this->assign('beautician', $beauticianInfo);
    			$this->assign('userId', $userId);
    			$this->assign('item', $orderInfo);
    			 
    			if ($comment) {
    				$comment ['start'] = floor($comment ['avg_score']);
    
    				$photo = M('comment_photo')->where(array('comment_id' => $comment ['id']))->select();
    				$commentPhoto = $OrderCommentModel->getCommentPhoto($comment ['id']);
    				$comment ['photos'] = $commentPhoto;
    				$comment ['user_info'] = getSessionUser();
    				
    				$reply = $OrderCommentModel->where(array('parentid' => $comment ['id']))->find();
    				if ($reply) {
    					$replyPhoto = $OrderCommentModel->getCommentPhoto($reply ['id']);
    					$reply ['photos'] = $replyPhoto;
    					$reply ['beautician'] = $beauticianInfo;
    					$comment ['reply'] = $reply;
    				}
    
    				$this->assign('comment', $comment);
    				$this->display('showComment');
    			} else {
    				$this->assign('number', md5(microtime()));
    				$this->display();
    			}
    		}
    	} else {
    		$orderId = I('post.post_id');
    		$OrderModel = D('Order');
    		$orderInfo = $OrderModel->getOrderInfo($orderId);
    		if ($orderInfo ['status'] != 4) {
    			$this->nodata('用户评价', '服务未使用，不能进行评论');
    		}
    
    		$OrderCommentModel = D('OrderComment');
    		$exitsComment = $OrderCommentModel->checkCommentStatus($orderId, I('post.uid'));
    		if (!$exitsComment) {
    			$result = $OrderCommentModel->addComment();
    			if (!$result) {
    				$this->nodata('用户评价', $OrderCommentModel->getError());
    			} else {
    				redirect(U('/Comments/comment/id/'.$orderId));
    			}
    		} else {
    			redirect(U('/Comments/comment/id/'.$orderId));
    		}
    	}
    }
    
	/**
	 * 查看评价
	 */
	public function replyComment() {
		$OrderId = I('get.id');
			
		$OrderModel = D('Order');
		$orderInfo = $OrderModel->getOrderInfo($OrderId);
		if ($orderInfo ['status'] != '4') {
			$this->nodata('查看评论', '该订单还不能评论');
		}
			
		$OrderCommentModel = D('OrderComment');
		$hasComment = $OrderCommentModel->where(array('post_id' => $OrderId))->count();
		if (!$hasComment) {
			$this->nodata('查看评论', '没有评论内容');
		}
			
		// 检测订单评论
		$comment = $OrderCommentModel->getCommentByOrderIdAndUid($OrderId, $orderInfo ['user_id']);
	
		$ServiceModel = D('ServiceItem');
		$service = $ServiceModel->getServiceInfo($orderInfo ['service_id']);
			
		$UserModel = D('User');
		$user_info = $UserModel->getUserById($comment ['uid']);
		$comment ['start'] = floor($comment ['avg_score']);
			
		$commentPhoto = $OrderCommentModel->getCommentPhoto($comment ['id']);
		$comment ['photos'] = $commentPhoto;
			
		$reply = $OrderCommentModel->where(array('parentid' => $comment ['id']))->find();
			
		if ($reply) {
			$replyPhoto = $OrderCommentModel->getCommentPhoto($reply ['id']);
			$reply ['photos'] = $replyPhoto;
			$this->assign('reply', $reply);
		}
			
		$this->assign('beautician', getBeautician());
		$this->assign('user_info', $user_info);
		$this->assign('service', $service);
		$this->assign('number', md5(microtime()));
		$this->assign('comment', $comment);
		$this->assign('order', $orderInfo);
			
		$this->display();
	}
}